
/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Timers;
using System.Threading;
using Pbdms.Common.CryptoUtils;

using Pbdms.Common.SharedVariables;
namespace Pbdms.BusinessLogic.Mail
{
    public class MailBoxManager
    {
        private IDictionary mailBoxs;
        public event EventHandler MailBoxAdded;
        public event EventHandler NewMessages;
        /// <summary>
        /// Indicates that refresh for a given mailbox(the source) is starts
        /// </summary>
        public event EventHandler RefreshStart;
        /// <summary>
        /// Indicates that refresh for a given mailbox(the source) is Done
        /// </summary>
        public event EventHandler RefreshDone;

        private SecretKeyControl keyControl;
        private PublicKeysControl publicKeyControl;
        public MailBoxManager()
        {
            mailBoxs= new Hashtable(); 
        }
        public MailBoxManager(SecretKeyControl myKeys, PublicKeysControl publicKeyControl)
        {
            this.keyControl = myKeys;
            this.publicKeyControl = publicKeyControl;
            mailBoxs = new Hashtable();
        }
        public IDictionary MailBoxs
        {
            get
            {
                return mailBoxs;
            }
            set
            {
                mailBoxs = value;
            }
        }
        /// <summary>
        /// load all mailboxs
        /// </summary>
        public void loadMailBoxs()
        {
            MailBox Inbox = new MailBox("Inbox",keyControl,publicKeyControl);
     
            mailBoxs.Add(Inbox.cacheKey, Inbox);
            setUpEventHandling();
            if (MailBoxAdded != null)
            {
                foreach (MailBox mailbox in mailBoxs.Values)
                {
                    MailBoxAdded(mailbox, EventArgs.Empty);
                }
            }
        }
        //to be implemented
        public void saveMessagesToCache()
        {


            // Only save feeds if there are any message
            IEnumerator enumerator = mailBoxs.Values.GetEnumerator();
            if (enumerator.MoveNext())
            {
                MailBox mailBox = (MailBox)enumerator.Current;
                mailBox.saveCompleteCache();

            }
            else
            {

            }


        
        }

        private void setUpEventHandling()
        {
            foreach (MailBox mailbox in mailBoxs.Values)
            {
                
                mailbox.messageManager.NewMessages+= new EventHandler(OnNewMessages);
                mailbox.messageManager.RefreshStart+= new EventHandler(OnRefreshStart);
                
                mailbox.messageManager.RefreshDone+= new EventHandler(OnRefreshDone);
            }
        }
        private void OnNewMessages(object sender, EventArgs e)
        {
            if (NewMessages != null)
            {
                NewMessages(sender, e);
            }
        }
        private void OnRefreshStart(object sender, EventArgs e)
        {
            if (RefreshStart != null)
            {
                RefreshStart(sender, e);
            }
        }

        private void OnRefreshDone(object sender, EventArgs e)
        {
            if (RefreshDone != null)
            {
                RefreshDone(sender, e);
            }
        }
        public void refreshInbox()
        {
            MailBox mailbox = (MailBox)mailBoxs["Inbox"];
            mailbox.messageManager.RefreshInbox();
        }
    }

}
